package com.sgb.kjwl.view.ui.fragment.self;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMachineStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.user.MachineStateEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.activity.self.publish.LeaseMachineActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.MachineListActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.MachineStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.MachineStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class MachineStateFragment extends AppFragment {
    public static final String TAG = "";
    public static final String KEY_TYPE = "type";
    public static final String KEY_STATE = "state";
    @Inject
    MachineStateModel viewModel;
    private FragmentMachineStateBinding binding;
    private String state = "";
    private MachineStateAdapter machineStateAdapter;


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static MachineStateFragment newInstance(String state) {
        MachineStateFragment fragment = new MachineStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_STATE, state);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_machine_state, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        state = (String) getArguments().get(KEY_STATE);
        initView();
        initData();
    }

    private void initView() {
        if (machineStateAdapter == null) {
            machineStateAdapter = new MachineStateAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp10), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(machineStateAdapter);
        machineStateAdapter.setOnItemClick(new AdapterOnItemClick<MachineStateEntity>() {
            @Override
            public void onItemClick(MachineStateEntity machineStateEntity, int position) {
                viewModel.getH5Url(machineStateEntity.getId() + "&iosFlag=2");
            }
        });
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getMachineOutModel(state, ((MachineListActivity) getActivity()).getKeyWord());
            }

            @Override
            public void onLoadMore() {
                viewModel.getMachineOutMoreModel(state, ((MachineListActivity) getActivity()).getKeyWord());
            }
        });
        machineStateAdapter.setOptCallBack(new MachineStateAdapter.OptCallBack() {
            @Override
            public void del(String id) {
                viewModel.delMachineOutModel(id);
            }

            @Override
            public void onlinOff(String id, String state) {
                viewModel.machineOutOnlinOffModel(id, state);
            }

            @Override
            public void edit(MachineStateEntity entity) {
                LeaseMachineActivity.start(getActivity(), entity.getId());
            }
        });
    }

    private void initData() {
        viewModel.getMachineOutModel(state, ((MachineListActivity) getActivity()).getKeyWord());
    }

    public void onlineOffSuccess(String state) {
        if (state.equals("1")) {
            MToast.showToast(getActivity(), "操作成功!");
        } else if (state.equals("2")) {
            MToast.showToast(getActivity(), "操作成功!");
        }
        EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_MACHINE_REFUSH));
    }

    public void delSuccess() {
        MToast.showToast(getActivity(), "删除成功!");
        EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_MACHINE_REFUSH));
    }

    public void refresh(List<MachineStateEntity> datas) {
        binding.emptyChild.emptyView.setVisibility(View.GONE);
        binding.rlvView.setVisibility(View.VISIBLE);
        binding.rlvView.setLoadingMoreEnabled(true);
        machineStateAdapter.setDatas(datas);
        binding.rlvView.refreshComplete();
        if (machineStateAdapter.getItemCount() >= viewModel.total) {
            binding.rlvView.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rlvView.refreshComplete();
        machineStateAdapter.setDatas(new ArrayList<>());
        binding.emptyChild.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<MachineStateEntity> datas) {
        machineStateAdapter.addDatas(datas);
        binding.rlvView.loadMoreComplete();
        if (machineStateAdapter.getItemCount() >= viewModel.total) {
            binding.rlvView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreComplete() {
        binding.rlvView.loadMoreComplete();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MachinePublishEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_MACHINE_REFUSH) {
            viewModel.getMachineOutModel(state, ((MachineListActivity) getActivity()).getKeyWord());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
